package com.web.servlet;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;


/**
 * @descrition :
 * @version 1.0
 * @date :
 * @author : Carey
 */

public abstract class BaseController {

	protected final Log logger = LogFactory.getLog(getClass());
	
	protected static final int PRIVATE_GROUP_MAXUSER=50;
	protected static final boolean IS_PRIVATE_GROUP=false;
	protected static final boolean IS_PUBLIC_GROUP=true;
	protected static final boolean IS_APPROVAL=true;
	protected static final boolean NO_APPROVAL=false;
	
	
	
	public BaseController() {
		
		System.out.println("==============================================================");
	}


    protected   boolean isNumeric(String str){
        for (int i = 0; i < str.length(); i++){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }


    @Deprecated
    public   <T>T getLogin_info(Class<T> t, HttpServletRequest request){
        HttpSession httpSession = request.getSession();
        String  jSession=httpSession.getId();
        Object object=httpSession.getAttribute(jSession);
        return (T) object;
    }




    /**
     * 请求参数映射
     * 
     * @param request
     * @return
     */
    public Map<String, Object>[] request2Map(HttpServletRequest request) {
        Map<String, Object>[] mapsList = new HashMap[2];
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> mapObject = new HashMap<String, Object>();
        Map<String, Object> mapObjectList = new HashMap<String, Object>();
        for (String key : map.keySet()) {
            if (map.get(key).length == 1) {
                mapObject.put(key, map.get(key)[0]);
            }
            if (map.get(key).length > 1) {
                String[] strings = map.get(key);
                mapObjectList.put(key, strings);

            }
        }
        mapsList[0] = mapObject;
        mapsList[1] = mapObjectList;
        return mapsList;
    }


    /**
     * 把字符串中的空格替换为%
     * @param str
     * @return
     */
    public String replaceQueryBlank (String str) {
        if (str != null) {
            return str.trim().replaceAll("\\s+", "%");
        }
        return null;
    }
    


    /**
     * @descrition : 获取并计算分页数据
     * @param request
     * @return
     * @date :
     * @updateAuthor:
     */
    public  void   pageParam(HttpServletRequest request,Map<String,Object> map) {
        int pageSize = 5;
        int currentPage = 1;

        if(null != request.getParameter("page_num")) {
            currentPage = Integer.parseInt(request.getParameter("page_num"));
        }
        if(null != request.getParameter("page_size")) {
            pageSize = Integer.parseInt(request.getParameter("page_size"));
        }

        int offSize = 0;
        //mysql返回参数
        if(currentPage >0){
            offSize = (currentPage-1)*pageSize;
        }else{
            currentPage=1;
        }
        
        map.put("pageSize",pageSize);
        map.put("offSize",offSize);
        map.put("currentPage",currentPage);
    }



    /**
     *
     * @descrition : 页面分页封装 ， 页面写の
     * @param
     * @return
     * @date :
     * @updateAuthor: Carey
     */
    public  String[] get_delId(Object parameter, String symbol) {
        String parameters = parameter.toString();
        String[] strings = parameters.split(symbol);
        return  strings;
    }






}
